package com.zeng.sboot.demo.amqp.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * # 表示匹配1个或多个单词; * 表示匹配1个单词
 * 要先在后台创建 Queue 和 Exchange
 */
@Configuration
public class RabbitmqConfig {

    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    /**
     * Topic交换机：按规则转发消息
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     * @return
     */
    @Bean
    public Exchange exchangeTopicsInform() {
        //durable(true)持久化，消息队列重启后交换机仍然存
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    /**
     * 声明 一个 短信消息 队列
     * @return
     */
    @Bean
    public Queue queueInformSms() {
        return new Queue(QUEUE_INFORM_SMS);
    }

    /**
     * 声明 一个 邮件消息 队列
     * @return
     */
    @Bean
    public Queue queueInformEmail () {
        return new Queue(QUEUE_INFORM_EMAIL);
    }

    /**
     * 绑定队列到交换机（如编码中的 channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");）
     * @return
     */
    @Bean
    public Binding bindingQueueInformSms() {
        return BindingBuilder.bind(queueInformSms()).to(exchangeTopicsInform()).with("inform.#.sms.#").noargs();
    }

    /**
     * 绑定队列到交换机（如编码中的 channel.queueBind(QUEUE_INFORM_EMAIL,"inform_exchange_topic","inform.#.email.#");）
     * @return
     */
    @Bean
    public Binding bindingQueueInformEmail() {
        return BindingBuilder.bind(queueInformEmail()).to(exchangeTopicsInform()).with("inform.#.email.#").noargs();
    }

}
